package hsh.gui;

import hsh.command.CleanRoomCommad;
import hsh.command.Command;
import hsh.command.WashClothesCommand;
import hsh.kernel.OperatingSystemKernel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * Lost Again Shell
 */
public class LASh {

	private JPanel mainPane = null;

	public JPanel getMainPane() {
		return mainPane;
	}

	public void setMainPane(JPanel mainPane) {
		this.mainPane = mainPane;
	}

	public JTextArea getTextArea() {
		return textArea;
	}

	public void setTextArea(JTextArea textArea) {
		this.textArea = textArea;
	}

	public JLabel getHouse() {
		return house;
	}

	public void setHouse(JLabel house) {
		this.house = house;
	}

	public JLabel getDevice() {
		return device;
	}

	public void setDevice(JLabel device) {
		this.device = device;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getStandadCommmonUserText() {
		return standadCommmonUserText;
	}

	public void setStandadCommmonUserText(String standadCommmonUserText) {
		this.standadCommmonUserText = standadCommmonUserText;
	}

	public String getStandadRootUserText() {
		return standadRootUserText;
	}

	public void setStandadRootUserText(String standadRootUserText) {
		this.standadRootUserText = standadRootUserText;
	}

	public Hashtable<String, Command> getCommands() {
		return commands;
	}

	public void setCommands(Hashtable<String, Command> commands) {
		this.commands = commands;
	}

	public LashVisualInterface getGui() {
		return gui;
	}

	public void setGui(LashVisualInterface gui) {
		this.gui = gui;
	}

	public ArrayList<String> getLastCommands() {
		return lastCommands;
	}

	public void setLastCommands(ArrayList<String> lastCommands) {
		this.lastCommands = lastCommands;
	}

	public int getLastCommandsIndex() {
		return lastCommandsIndex;
	}

	public void setLastCommandsIndex(int lastCommandsIndex) {
		this.lastCommandsIndex = lastCommandsIndex;
	}

	public static boolean isMULTICOLORED() {
		return MULTICOLORED;
	}

	public static String getLOOKANDFEEL() {
		return LOOKANDFEEL;
	}

	public void setConsoleScroll(JScrollPane consoleScroll) {
		ConsoleScroll = consoleScroll;
	}

	private JScrollPane ConsoleScroll = null;

	private JTextArea textArea = null;
	private JLabel house = null;
	private JLabel device = null;
	private String userType;
	// private String standadText = "HSH OS: $> ";
	private String standadCommmonUserText = "HSH OS: $> ";
	private String standadRootUserText = "HSH OS: #> ";
	final static boolean MULTICOLORED = false;
	private Hashtable<String, Command> commands;
    private LashVisualInterface gui;
	private ArrayList<String> lastCommands;
	private int lastCommandsIndex;
	

	// Specify the look and feel to use. Valid values:
	// null (use the default), "Metal", "System", "Motif", "GTK+"
	final static String LOOKANDFEEL = null;

	public LASh(OperatingSystemKernel kernel,LashVisualInterface gui) {

		this.textArea = new JTextArea();
		this.lastCommands = new ArrayList<String>();
		this.lastCommandsIndex = 0;
		// remover isso depois
		// verificar tipo de usuario
		this.userType = "root";

		this.commands = new Hashtable<String, Command>();
		Command command = new CleanRoomCommad(kernel.getScheduler(),gui,"quarto");
		this.commands.put("clean", command);
		
		
		house = new JLabel(new ImageIcon("house2.jpg"));
		device = new JLabel(new ImageIcon("lavar.jpg"));
	}
	
	public LASh() {

		this.textArea = new JTextArea();
		this.lastCommands = new ArrayList<String>();
		this.lastCommandsIndex = 0;
		// remover isso depois
		// verificar tipo de usuario
		this.userType = "root";

		/*this.commands = new Hashtable<String, Command>();
		this.commands.put("washcl", new WashClothesCommand(kernel
				.getScheduler()));*/

		house = new JLabel(new ImageIcon("house2.jpg"));
		device = new JLabel(new ImageIcon("lavar.jpg"));
	}

	private void getEarlierCommands(java.awt.event.KeyEvent e) {
		if (e != null) {
			if (e.getKeyCode() == KeyEvent.VK_UP
					|| e.getKeyCode() == KeyEvent.VK_KP_UP) {
				e.consume();
			} else if (e.getKeyCode() == KeyEvent.VK_DOWN
					|| e.getKeyCode() == KeyEvent.VK_KP_DOWN) {
				e.consume();
			}
		}
	}

	private void PrintDefaultText(java.awt.event.KeyEvent e) {
		if (e != null) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				// e.consume();
				this.textArea.append(getStandardText());
				System.out.println("chegou aki 1");
			}
		} else {
			System.out.println("chegou aki 2");
		}
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	
	private static void initLookAndFeel() {
		String lookAndFeel = null;

		if (LOOKANDFEEL != null) {
			if (LOOKANDFEEL.equals("Metal")) {
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
			} else if (LOOKANDFEEL.equals("System")) {
				lookAndFeel = UIManager.getSystemLookAndFeelClassName();
			} else if (LOOKANDFEEL.equals("Motif")) {
				lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			} else if (LOOKANDFEEL.equals("GTK+")) { // new in 1.4.2
				lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
			} else {
				System.err
						.println("Unexpected value of LOOKANDFEEL specified: "
								+ LOOKANDFEEL);
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
			}

			try {
				UIManager.setLookAndFeel(lookAndFeel);
			} catch (ClassNotFoundException e) {
				System.err
						.println("Couldn't find class for specified look and feel:"
								+ lookAndFeel);
				System.err
						.println("Did you include the L&F library in the class path?");
				System.err.println("Using the default look and feel.");
			} catch (UnsupportedLookAndFeelException e) {
				System.err.println("Can't use the specified look and feel ("
						+ lookAndFeel + ") on this platform.");
				System.err.println("Using the default look and feel.");
			} catch (Exception e) {
				System.err.println("Couldn't get specified look and feel ("
						+ lookAndFeel + "), for some reason.");
				System.err.println("Using the default look and feel.");
				e.printStackTrace();
			}
		}
	}

	public boolean executeCommands(String command) {
		
		
		if(command != null && !command.equals("")){
			this.lastCommands.add(command);
			
			//commands.get(command).execute();
			Command c = commands.get(command);
			if (c != null)
				c.execute();
			else{
				//System.out.println("Fudeu, melhorar isso depois :D");
				return false;
			}
		}

		return true;
	}

	public String lastCommandsNavigate(boolean up) {
		if (up) {
			if (this.lastCommandsIndex > 0) {
				lastCommandsIndex--;
				return this.lastCommands.get(lastCommandsIndex);
			} else
				return "";
		} else {
			if (this.lastCommandsIndex < this.lastCommands.size()) {
				return this.lastCommands.get(lastCommandsIndex++);
			} else {
				return this.lastCommands.get(lastCommandsIndex - 1);
			}// else return "";
		}
	}

	public String getStandardText() {
		if (userType.equalsIgnoreCase("root"))
			return standadRootUserText;
		else
			return standadCommmonUserText;
	}

	
	
	
}
